package com.hundsun.cprs.yyt.redis;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import redis.clients.jedis.Jedis;

public class Producer extends Thread{
    public static final String MESSAGE_KEY = "message:queue";
    private Jedis jedis;
    private String producerName;
    private volatile int count;
    
    public Producer(String name){
        this.producerName = name;
        init();
    }
    
    private void init(){
        jedis = new Jedis("192.168.78.101", 6379); 
        jedis.auth("redis@123");
    }
    
    public void sendMessage(String message){
        Long size = jedis.lpush(MESSAGE_KEY, message);
        System.out.println("添加消息成功,当前消息数：" + size);
        count++;
    }

    @Override
    public void run() {
        
        while(true){
            try {
                sendMessage(UUID.randomUUID().toString() + "/" + System.currentTimeMillis());
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                 e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        Producer producer = new Producer("test-message");
        producer.sendMessage(UUID.randomUUID().toString() + "/" + System.currentTimeMillis());
    }
    
    
}
